/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {ListNode}
 * @description 用map来寄存每一次的节点 最后找到环形链表的开头 后面那一个 即为答案
 * 然后发现其实根本用不到map 愚昧啊...
 */

var detectCycle = function (head) {
    let map = new Map()
    while (head) {
        if (head.flag) {
            return map.get(head.index)
        }
        head.flag = true
        head.index = map.size
        map.set(head.index, head)
        head = head.next
    }
    return null
};